package com.ultrawise.survey.module.surevybase.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ultrawise.survey.contants.SurveyConstants;
import com.ultrawise.survey.core.base.BaseContoller;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.surevybase.service.TSurveyInfoBaseService;
import com.ultrawise.survey.module.surevybase.service.TSurveyInfoObjtableService;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoBase;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoObjtable;

@Controller
@RequestMapping(value = "/symanage/objectable")
public class TSurveyInfoObjtableControl extends BaseContoller {
	@Autowired
	TSurveyInfoObjtableService tSurveyInfoObjtableService;
	@Autowired
	TSurveyInfoBaseService tsurveyinfobaseservice;
	/***
	 * 添加对象资料
	 * @param objtab
	 * @param request
	 * @return
	 */	
	@RequestMapping(value = "addobj.htm")
	public ModelAndView addobj(TSurveyInfoObjtable objtab,HttpServletRequest request) {
		List<TSurveyInfoObjtable> ls = new ArrayList<TSurveyInfoObjtable>();
		TSurveyInfoBase  bs = new TSurveyInfoBase();
		try {
			 bs = (TSurveyInfoBase) tsurveyinfobaseservice.queryByID(objtab.getSy_id(), new TSurveyInfoBase(), TSurveyInfoBase.class);
			 ls = (List<TSurveyInfoObjtable>) tSurveyInfoObjtableService.query("SY_ID", objtab.getSy_id().toString(), objtab, TSurveyInfoObjtable.class);
		
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		request.setAttribute("bs", bs);
		request.setAttribute("obtype",SurveyConstants.OBJECT_TYPEMAP);
		return new ModelAndView("/objectable/addobj","ls",ls);
	}
	
	@RequestMapping(value = "saveobj.htm")
	public ModelAndView saveobj(TSurveyInfoObjtable objtab,
			HttpServletRequest request) {
		try {
			//如果该节点为空，表示没有归属的问卷信息，所以不予添加操作
			if(objtab!=null && objtab.getSy_id()!=null){
				objtab.setId((Integer) tSurveyInfoObjtableService.getMaxID(objtab) + 1);
				objtab.setOb_isuse(0);//设置是否其用途0:启动状态   1:停止状态
				//转化成“|”为风格符号
				if(objtab.getOb_type()!=null && "7".equalsIgnoreCase(objtab.getOb_type())){
					String defvalue = objtab.getOb_default().replaceAll("\r\n",SurveyConstants.SURVEY_DATAINFO_SPACE);
					objtab.setOb_default(defvalue);
				}else{
					String defvalue = objtab.getOb_default();
					defvalue=(defvalue!=null&&defvalue.indexOf(",")>=0)?defvalue.substring(0,defvalue.indexOf(",")):defvalue;
					objtab.setOb_default(defvalue);
				}
				
				if(objtab.getOb_type().equals("1")){
					if(objtab.getOb_size()==null ||objtab.getOb_size()<=0 )
						objtab.setOb_size(20);//设置默认值为20
				}
				tSurveyInfoObjtableService.saveObj(objtab);
			}else{
				request.setAttribute("message", "非法操作，请返回。");
				request.setAttribute("url", request.getContextPath() + "/symanage/baseinfo/list.htm");
				return new ModelAndView("/massage");
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return new ModelAndView("redirect:/symanage/objectable/addobj.htm?sy_id="+objtab.getSy_id());
	}	
	
	// 删除
	@RequestMapping(value = "deleobj.htm")
	public ModelAndView deleobj(TSurveyInfoObjtable objtab,	HttpServletRequest request) {
		if (objtab != null && objtab.getId() != null) {
			try {
				TSurveyInfoObjtable objt=(TSurveyInfoObjtable) tSurveyInfoObjtableService.queryByID(objtab.getId(), new TSurveyInfoObjtable(), TSurveyInfoObjtable.class);
				if(!tSurveyInfoObjtableService.isExitTable(objt.getSy_id())){
					tSurveyInfoObjtableService.removeObj(objtab.getId(), objtab);
					request.setAttribute("message", "问卷已经修改。");
				}else{
					request.setAttribute("message", "删除失败，该问卷已经发布，字段只能停用。");
				}
			} catch (ServiceException e) {
				e.printStackTrace();
				request.setAttribute("message", "删除失败，请重试或者联系管理员。");
			}
		}else{
			request.setAttribute("message", "非法操作，请返回。");
		}
		request.setAttribute("url", request.getContextPath()
				+ "/symanage/objectable/addobj.htm?sy_id="+ objtab.getSy_id());
		return new ModelAndView("/massage");
	}
	
	/****
	 * 编辑前的页面获取对象信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "eidobj.htm")
	public ModelAndView eidobj(HttpServletRequest request, TSurveyInfoObjtable objinfo, ModelMap map) {

		TSurveyInfoObjtable tobj = new TSurveyInfoObjtable();
		List<TSurveyInfoObjtable> ls = new ArrayList<TSurveyInfoObjtable>();
		TSurveyInfoBase  bs = new TSurveyInfoBase();
		try {
			
			// 编辑前查询对应的id的问卷信息表
			tobj = (TSurveyInfoObjtable) tSurveyInfoObjtableService.queryByID(objinfo.getId(), new TSurveyInfoObjtable(),
					TSurveyInfoObjtable.class);
			bs = (TSurveyInfoBase) tsurveyinfobaseservice.queryByID(tobj.getSy_id(), new TSurveyInfoBase(), TSurveyInfoBase.class);
			//判断类型为7的就做竖线分割变化。
			if("7".equals(tobj.getOb_type())){
				tobj.setOb_default(tobj.getOb_default().replaceAll(SurveyConstants.SURVEY_DATAINFO_SPACE,"\r\n"));
			}
			 ls = (List<TSurveyInfoObjtable>) tSurveyInfoObjtableService.query("SY_ID", tobj.getSy_id().toString(), tobj, TSurveyInfoObjtable.class);
			} catch (ServiceException e) {
			e.printStackTrace();
		}
		map.put("tobj", tobj);
		map.put("bs", bs);
		map.put("ls", ls);
		request.setAttribute("obtype",SurveyConstants.OBJECT_TYPEMAP);
		return new ModelAndView("/objectable/eidobj",map);
	}
	
	 
	/***
	 * 具体更新数据库信息
	 * @param objtab
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "editobj.htm")
	public ModelAndView editobj(TSurveyInfoObjtable objtab,	HttpServletRequest request) {
		try {
			//如果该节点为空，表示没有归属的问卷信息，所以不予添加操作
			if(objtab!=null && objtab.getId()!=null){
				
				//类型为字符串的时候，长度默认为10
				if(objtab.getOb_type().equals("1")){
					if(objtab.getOb_size()==null ||objtab.getOb_size()<=0 )
						objtab.setOb_size(20);//设置默认值为20
				}
				//转化成“|”为风格符号
				if(objtab.getOb_type()!=null && "7".equalsIgnoreCase(objtab.getOb_type())){
					String defvalue = objtab.getOb_default().replaceAll("\r\n",SurveyConstants.SURVEY_DATAINFO_SPACE);
					objtab.setOb_default(defvalue);
				}else{
					String defvalue = objtab.getOb_default();
					defvalue=(defvalue!=null&&defvalue.indexOf(",")>=0)?defvalue.substring(0,defvalue.indexOf(",")):defvalue;
					objtab.setOb_default(defvalue);
				}
				
				tSurveyInfoObjtableService.updateObj(objtab);
			}else{
				request.setAttribute("message", "非法操作，请返回。");
				request.setAttribute("url", request.getContextPath() + "/symanage/baseinfo/list.htm");
				return new ModelAndView("/massage");
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return new ModelAndView("redirect:/symanage/objectable/addobj.htm?sy_id="+objtab.getSy_id());
	}
	
	
	
	@RequestMapping(value = "isuse.htm")
	public ModelAndView isuse(TSurveyInfoObjtable objtab,HttpServletRequest request) {
		if (objtab != null && objtab.getId() != null) {
			try {
				TSurveyInfoObjtable objt=(TSurveyInfoObjtable) tSurveyInfoObjtableService.queryByID(objtab.getId(), new TSurveyInfoObjtable(), TSurveyInfoObjtable.class);
				if(objt!=null && objt.getOb_isuse()!=null){
					if(objt.getOb_isuse()==0){
						objt.setOb_isuse(1);
					}else{
						objt.setOb_isuse(0);
					}
					objtab.setSy_id(objtab.getSy_id());
					tSurveyInfoObjtableService.updateObj(objt);
					request.setAttribute("message", "该对象的字段状态已经修改成功。");
				}
			} catch (ServiceException e) {
				e.printStackTrace();
				request.setAttribute("message", "操作失败，请重试或者联系管理员。");
			}
		}else{
			request.setAttribute("message", "非法操作，请返回。");
		}
		request.setAttribute("url", request.getContextPath()
				+ "/symanage/objectable/addobj.htm?sy_id="+ objtab.getSy_id());
		return new ModelAndView("/massage");
	}

}